package com.lumlord.common.web;


import cn.hutool.core.bean.BeanUtil;
import com.lumlord.common.base.SERVER;
import com.lumlord.common.base.ServerRibbon;
import com.lumlord.common.domain.Result;
import com.lumlord.common.domain.log.monitor.Breakdown;
import com.lumlord.common.exception.BusinessException;
import com.lumlord.common.shiro.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

//@Component
public class WebSocket {
    /**
     *  web socket 通知
     * @param userId 用户 id
     * @param message  消息
     * @param page 页面地址
     * @param modeName 模块名称
     * @param attrs  附加属性
     * @return
     * @throws BusinessException
     */
    public Result notice(Integer userId,String message, String page, String modeName, Map attrs) throws BusinessException {
        Map<String,Object> args = new HashMap<String,Object>();
        Map<String,Object> val = new HashMap<String,Object>();
        val.put("message",message);
        if(null != page){
            val.put("page",page);
        }
        val.put("modeName",modeName);
        val.put("attrs",attrs);
        args.put("message",val);
        //return serverRibbon.callPost(SERVER.monitorServer,"websocket/push/"+ userId,args);
        return null;
    }

    /**
     *  web socket 通知
     * @param userId 用户 id
     * @param message  消息
     * @param modeName 模块名称
     * @param attrs  附加属性
     * @return
     * @throws BusinessException
     */
    public Result notice(Integer userId,String message, String modeName, Map attrs) throws BusinessException {
        return notice(userId,message,null,modeName,attrs);
    }
    /**
     *  web socket 通知
     * @param userId 用户 id
     * @param message  消息
     * @param modeName 模块名称
     * @return
     * @throws BusinessException
     */
    public Result notice(Integer userId,String message,  String modeName) throws BusinessException {
        return notice(userId,message,null,modeName,null);
    }
    /**
     *  web socket 通知
     * @param message  消息
     * @param modeName 模块名称
     * @return
     * @throws BusinessException
     */
    public Result notice(String message,  String modeName) throws BusinessException {
        Integer userId = ShiroUtils.getUserId();
        return notice(userId,message,null,modeName,null);
    }

    public Result breakDownNotice(Breakdown breakdown ) throws BusinessException{
        Map<String,Object> args = new HashMap<String,Object>();
        Map<String,Object> val = new HashMap<String,Object>();
        val.put("message",breakdown.getDescription());
        val.put("modeName","main");
        val.put("attrs", BeanUtil.beanToMap(breakdown));
        args.put("message",val);
        args.put("areaId",breakdown.getAreaId());
       //  serverRibbon.callPost(SERVER.monitorServer,"websocket/push/area/"+ breakdown.getAreaId(),args);
        //return serverRibbon.callPost(SERVER.monitorServer,"websocket/push/null",args);
        return null;
    }



    @Autowired
    private ServerRibbon serverRibbon;
}